package cn.tony.buildsrc

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.publish.PublicationContainer
import org.gradle.api.publish.PublishingExtension
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin
import org.gradle.api.tasks.bundling.Jar

/**
 *  作用在module  build.gradle.kts
 *
 */
class PushPlugin : Plugin<Project> {

    lateinit var project: Project

    override fun apply(project: Project) {
        this.project = project
        println("========================================name===${project.name}")
        //
        project.plugins.apply(MavenPublishPlugin::class.java)
//        project.plugins.apply(SigningPlugin::class.java)

        project.plugins.forEach {
            println(it::class.java)
        }

        addTask()

        project.extensions.configure(PublishingExtension::class.java) {
            project.afterEvaluate {
                publications {
                    allPublication(project)
                }
                repositories {
                    maven { url = uri(properties["localMavenPath"].toString()) }
                }
            }
        }

    }

    private fun addTask() {
        if (isAndroidApk(project) || isAndroidLib(project)) {
            project.tasks.register("androidSourcesJar", Jar::class.java) {
                dependsOn(project.getTasksByName("classes", false))
                archiveClassifier.set("sources")
                from("android.sourceSets.main.java.source")
            }

        } else {
            project.tasks.register("sourcesJar", Jar::class.java) {
                dependsOn(project.getTasksByName("classes", false))
                archiveClassifier.set("sources")
                from("sourceSets.main.allSource")
            }
        }

    }

    private fun PublicationContainer.allPublication(project: Project) {
        if (isDebug(project)) {
            if (isAndroidApk(project)) {
                createPublication(this, "debug_aab")
            }
            if (isAndroidLib(project)) {
                createPublication(this, "debug")
            }
        } else {
            if (isAndroidApk(project)) {
                createPublication(this, "release_aab")
            }
            if (isAndroidLib(project)) {
                createPublication(this, "release")
            }
        }

        if (isJavaLib(project)) {
            createPublication(this, "java")
        }
        if (isKotlinLib(project)) {
            createPublication(this, "kotlin")
        }
    }

    /**
     *  source 还有待改进
     */
    private fun createPublication(
        publicationContainer: PublicationContainer,
        componentName: String,
        jarSource: Boolean = false
    ) {
        publicationContainer.create(componentName, MavenPublication::class.java) {
            version = project.properties["VERSION_NAME"].toString()
            project.group = project.properties["GROUP"].toString()
//            artifactId = project.properties["POM_NAME"].toString()
            artifactId = project.name
            println(project.components.names)
            project.components.forEach {
                println("components============${it.name}")
            }

            from(project.components.findByName(componentName))
            if (jarSource) {
                val taskName: String =
                    if (isAndroidApk(project) || isAndroidLib(project)) "androidSourcesJar"
                    else "sourcesJar"
                val findByName = project.tasks.findByName(taskName)
                println("==================================$findByName")
                artifact(findByName)
            }

            pom {
                name.set("My Library")
                description.set("A concise description of my library")
                url.set("http://www.example.com/library")
                properties.set(
                    mapOf(
                        "myProp" to "value",
                        "prop.with.dots" to "anotherValue"
                    )
                )

                licenses {
                    license {
                        license {
                            name.set("The Apache License, Version 2.0")
                            url.set("http://www.apache.org/licenses/LICENSE-2.0.txt")
                        }
                    }
                }

                developers {
                    developer {
                        id.set("tonyl")
                        name.set("Tony Lee")
                        email.set("tony.lee@example.com")
                    }
                }

                scm {
                    connection.set("scm:git:git://example.com/my-library.git")
                    developerConnection.set("scm:git:ssh://example.com/my-library.git")
                    url.set("http://example.com/my-library/")
                }
            }
        }
    }
}

fun isDebug(project: Project): Boolean = project.property("DEBUG").toString() == "true"

//[all, debug, release]
fun isAndroidLib(project: Project): Boolean =
    project.plugins.hasPlugin("com.android.library")

//[debug_aab, debug_apk, release_aab, release_apk]
fun isAndroidApk(project: Project): Boolean =
    project.plugins.hasPlugin("com.android.application")

fun isJavaLib(project: Project): Boolean =
    (project.plugins.hasPlugin("java-library") && !project.plugins.hasPlugin("kotlin"))

fun isKotlinLib(project: Project): Boolean =
    project.plugins.hasPlugin("kotlin")


